﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebComponents.Data;
using WebComponents;
using System.Collections.Generic;

public partial class chi_tiet_san_pham : System.Web.UI.Page {
  public string largeimg = "";
  public string pName = "";
  public string smallimg = "";
  protected void Page_Load(object sender, EventArgs e) {
    ProductInfo p = ProductController.GetByProductID(ConvertType.ToInt(Request.Params["id"]));
    lblTitle.Text = "<a href=\"san-pham-.aspx\" class=\"link_path\">Sản phẩm</a>";
    if (p != null) {
      ProductController.UpdateViewCount(p.ProductID);
      p.ViewCount += 1;
      largeimg = Page.ResolveClientUrl("~/") +  p.ImageUrl;
      smallimg = Page.ResolveClientUrl("~/") + p.SmallImageUrl;
      this.Title = pName = p.ProductName;
      lblContent.Text = p.FullDescription;
      lblViewCount.Text = p.ViewCount.ToString();
      lblSoLuongCon.Text = p.Comments;
      lblPrice.Text = p.UnitPrice.ToString("###,###,###") + " VNĐ";
      ManufacturerInfo m = ManufacturerController.GetByManuID(p.ManufacturerID);
      if (m != null)
        lblHangSanXuat.Text = m.ManufacturerVi;
      CategoryInfo cate = CategoryController.GetByCategoryID(p.CateID);
      if (cate != null)
        lblCate.Text = cate.NameVi;
      lblMaSanPham.Text = p.ProductCode;
      rptNhomGia.DataSource = ProductAttributesController.GetByProductID(p.ProductID);
      rptNhomGia.DataBind();
      if (cate != null) {

        CategoryInfo parent1 = CategoryController.GetByCategoryID(cate.ParentID);
        CategoryInfo parent2 = null;
        CategoryInfo parent3 = null;
        if (parent1 != null && parent1.ParentID > 0) {
          parent2 = CategoryController.GetByCategoryID(parent1.ParentID);
          if (parent2 != null && parent2.ParentID > 0) {
            parent3 = CategoryController.GetByCategoryID(parent2.ParentID);
          }
        }
        if (parent3 != null) {
          lblTitle.Text += " > " + string.Format("<a href=\"{0}san-pham/{1}/{2}-.aspx\" class=\"link_path\">{3}</a>", Page.ResolveClientUrl("~/"), parent3.CategoryID, ConvertType.StripDiacritics(parent3.NameVi), parent3.NameVi);
        }
        if (parent2 != null) {
          lblTitle.Text += " > " + string.Format("<a href=\"{0}san-pham/{1}/{2}-.aspx\" class=\"link_path\">{3}</a>", Page.ResolveClientUrl("~/"), parent2.CategoryID, ConvertType.StripDiacritics(parent2.NameVi), parent2.NameVi);
        }
        if (parent1 != null) {
          lblTitle.Text += " > " + string.Format("<a href=\"{0}san-pham/{1}/{2}-.aspx\" class=\"link_path\">{3}</a>", Page.ResolveClientUrl("~/"), parent1.CategoryID, ConvertType.StripDiacritics(parent1.NameVi), parent1.NameVi);
        }
        lblTitle.Text += " > " + string.Format("<a href=\"{0}san-pham/{1}/{2}-.aspx\" class=\"link_path\">{3}</a>", Page.ResolveClientUrl("~/"), cate.CategoryID, ConvertType.StripDiacritics(cate.NameVi), cate.NameVi);
        lblTitle.Text += " > " + string.Format("<a href=\"#\" class=\"link_path\">{0}</a>",p.ProductName);

      }
    }
    rptProducts.ItemDataBound += new RepeaterItemEventHandler(rptProducts_ItemDataBound);
    int id = ConvertType.ToInt(Request.Params["cid"]);
    List<ProductInfo> list = new List<ProductInfo>();
    list = ProductController.RamdomProduct(5, id, "vi-VN");
    rptProducts.DataSource = list;
    rptProducts.DataBind();
  }

  void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    ProductInfo p = e.Item.DataItem as ProductInfo;
    if (p != null) {
      Image img = e.Item.FindControl("imgDescription") as Image;
      Repeater rptNhomGia = e.Item.FindControl("rptNhomGia") as Repeater;
      if (p.ProductID > 0) {
        img.ImageUrl = p.SmallImageUrl;
        rptNhomGia.DataSource = ProductAttributesController.GetByProductID(p.ProductID);
        rptNhomGia.DataBind();
      } else
        img.Visible = false;
    }
  }
}
